package cn.kgc.vuedemoebook2.service.impl;

import cn.kgc.vuedemoebook2.mapper.EntryMapper;
import cn.kgc.vuedemoebook2.pojo.CategoryExample;
import cn.kgc.vuedemoebook2.pojo.Entry;
import cn.kgc.vuedemoebook2.pojo.EntryExample;
import cn.kgc.vuedemoebook2.service.EntryServcie;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author niejianhang
 * @create 2020-05-18 16:52
 */
@Service
public class EntryServcieImpl implements EntryServcie {

    @Resource
    EntryMapper entryMapper;

    @Override
    public PageInfo<Entry> selAllBycategoryId(Integer pageNum, Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);

        EntryExample entryExample=new EntryExample();
        EntryExample.Criteria criteria = entryExample.createCriteria();
        List<Entry> list;
        if(categoryId!=0){
            criteria.andCategoryidEqualTo(categoryId);
            list = entryMapper.selectByExample(entryExample);
        }else {
            list = entryMapper.selectByExample(null);
        }
        PageInfo<Entry> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int add(Entry entry) {
        int insert = entryMapper.insert(entry);

        return insert;
    }

    @Override
    public int del(Integer id) {
        int i = entryMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int update(Entry entry) {
        int i = entryMapper.updateByPrimaryKeySelective(entry);
        return i;
    }

    @Override
    public Entry selById(Integer id) {
        Entry entry = entryMapper.selectByPrimaryKey(id);
        return entry;
    }
}
